//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "GlassesModelDlg.h"
#include "FormUtilities.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CSPIN"
#pragma resource "*.dfm"

//---------------------------------------------------------------------------

__fastcall TfrmGlassesModelDlg::TfrmGlassesModelDlg(TComponent* Owner, TStrings * models)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->Caption = "Add new model";
   this->models = models;
   model = NULL;
   edtPrice->Text = "0";
   edtBought->Text = "0";
   edtSold->Text = "0";
   lblCurrency->Caption = "(" + CurrencyString + ")";
   lblRemainingValue->Caption = GetRemaining();
}

//---------------------------------------------------------------------------

__fastcall TfrmGlassesModelDlg::TfrmGlassesModelDlg(TComponent* Owner, TStrings * models, TGlassesModel * model)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);
   
   this->Caption = "Update model";
   this->models = models;
   this->model = model;

   edtName->Text = model->name;
   edtPrice->Text = FloatToStr( model->price);
   edtBought->Text = model->bought;
   edtSold->Text = model->sold;
   lblCurrency->Caption = "(" + CurrencyString + ")";
   lblRemainingValue->Caption = GetRemaining();
}

bool TfrmGlassesModelDlg::ValidatePrice( TEdit * price)
{
   float tmp;

   try
   {
      if ( price->Text.Length() == 0)
         throw new EConvertError( "Price should be entered !");

      try
      {
         tmp = StrToFloat( price->Text);
      }
      catch( EConvertError * e)
      {
         throw new EConvertError( "Price should be a decimal number greater than 0 !");
      }

      if ( tmp < 0)
         throw new EConvertError( "Price should be a decimal number greater than 0 !");

      return true;
   }
   catch( EConvertError * e)
   {
      MessageDlg( e->Message,
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);

      price->SetFocus();
      ModalResult = mrNone;
      return false;
   }
}

bool TfrmGlassesModelDlg::ValidateAmount( TCSpinEdit * amount)
{
   int tmp;

   try
   {
      try
      {
         tmp = StrToInt( amount->Text);
      }
      catch( EConvertError * e)
      {
         throw new EConvertError( "Amount should be a numeric value greater than 0 !");
      }

      if ( tmp < 0)
         throw new EConvertError( "Amount should be a numeric value greater than 0 !");

      return true;
   }
   catch( EConvertError * e)
   {
      MessageDlg( e->Message,
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);

      amount->SetFocus();
      ModalResult = mrNone;
      return false;
   }
}


//---------------------------------------------------------------------------

void __fastcall TfrmGlassesModelDlg::btnOKClick(TObject *Sender)
{
   float price;
   int bought, sold;

   if ( edtName->Text.Length() == 0)
   {
      MessageDlg( "Model name should be entered !",
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);

      edtName->SetFocus();
      ModalResult = mrNone;
      return;
   }

   if ( !ValidatePrice( edtPrice))
      return;

   price = StrToFloat( edtPrice->Text);

   if ( !ValidateAmount( edtBought))
      return;
           
   bought = StrToInt( edtBought->Text);

   if ( !ValidateAmount( edtSold))
      return;

   sold = StrToInt( edtSold->Text);

   if ( bought < sold)
   {
      MessageDlg( "Bought amount cannot be less than sold amount !",
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);

      edtBought->SetFocus();
      ModalResult = mrNone;
   }

   if ( model == NULL)
   {
      model = new TGlassesModel( -1, edtName->Text, price, bought, sold);
      models->AddObject( edtName->Text, model);
   }
   else
   {
      model->name   = edtName->Text;
      model->price  = price;
      model->bought = bought;
      model->sold   = sold;
   }
}
//---------------------------------------------------------------------------


AnsiString TfrmGlassesModelDlg::GetRemaining()
{
   int bought;
   int sold;

   try
   {
      bought = StrToInt( edtBought->Text);
   }
   catch ( EConvertError * e)
   {
      return "";
   }//try-catch

   try
   {
      sold = StrToInt( edtSold->Text);
   }
   catch ( EConvertError * e)
   {
      return "";
   }//try-catch

   return StrToInt( bought - sold);
}

void __fastcall TfrmGlassesModelDlg::edtQuantityChange(TObject *Sender)
{
   lblRemainingValue->Caption = GetRemaining();
}
//---------------------------------------------------------------------------

